<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>员工管理</title>
<!-- 导入jquery核心类库 -->
 <%@include file="../include/jqueryeasyui.jsp"%>

<script type="text/javascript">




	function doAdd(){
	
		$('#outputWindow').form('clear');
		$('#outputWindow *').trigger('blur');// 离焦
		$('#outputWindow').window("open").window('setTitle', '线下出库');
	}
	
	
	 //增加查询参数，重新加载表格  
    function reloadgrid() {  
  
        //查询参数直接添加在queryParams中      
        var queryParams = $('#grid').datagrid('options').queryParams;  
        queryParams.oOReceiverName = $('#searchcontent').val();  
        
        $('#grid').datagrid('options').queryParams = queryParams;  
        $("#grid").datagrid('load');  
    }  
	 
	function doSearch(){
		reloadgrid();
	}
	
	function doEdit(){
		var rowData = $('#grid').datagrid('getSelected');  
        
        if (rowData == null) {  
            $.messager.alert("提示", "请选择要修改的行！", "info");  
        } else{
	        $("input[name='sId']").val(rowData.sId);
			$("input[name='sName']").val(rowData.sName);
			$("input[name='sEmail']").val(rowData.sEmail);
			$("input[name='sPhone']").val(rowData.sPhone);
			$('#sSex').combobox('setValue', rowData.sSex);
			$('#sStatus').combobox('setValue', rowData.sStatus);
	 		$('#dept').combobox('select', rowData.sDeptId);

			// 弹出窗口
			  
			$("#addStandardWindow").window('open').window('setTitle', '修改员工');
        } 
	}
	

	// 定义列
	var columns = [ [ {
		field : 'oOTime',
		title : '出库时间',
		width : 120,	
		formatter:formatterDate
	}, {
		field : 'oOPNumber',
		title : '出库数量',
		width : 60,
	}, {
		field : 'oOKuGuanName',
		title : '经手人',
		width : 60,
	}, {
		field : 'oOReceiverName',
		title : '接收者',
		width : 60,
	}, {
		field : 'oOType',
		title : '去向',
		width : 120,
		formatter:formateroOType
	}, {
		field : 'oOBuyerName',
		title : '客户名称',
		width : 150,
	}, {
		field : 'oOPCodes',
		title : 'E尺SN',
		width : 160,
	}, {
		field : 'oOReMark',
		title : '备注',
		width : 160,
	}, {
		field : 'oOIsDone',
		title : '是否办理',
		width : 60,
		formatter:formateroOIsDone
	}, {
		field : 'oOid',
		title : '操作',
		width : 160,
		formatter:formatterCaoZuo
	} ] ];
	
	function formatterCaoZuo(value,row,index){
		if(row.oOIsDone==false){

			return "<a onclick=doProcess("+index+") href=#>处理</a>&nbsp;&nbsp;&nbsp;&nbsp;<a onclick=doDel("+index+") href=#>删除</a>";
		}else{
			return "";
		}
	}	
	
	function formateroOType(value,row,index){
		if(value==0){
			return "员工自用";
		}else if(value==1){
			return "订单出货";
		}
	}
	
	function doDel(index){
	    $('#grid').datagrid('selectRow',index);// 关键在这里  
	    var row = $('#grid').datagrid('getSelected'); 
	    if(row){ 
	/*     	$.messager.alert('消息','确认删除 当前记录？','info',function(){
	     	   
			})
			 */
		     $.messager.confirm("操作提示", "确认删除 当前记录？", function (data) {  
		            if (data) {  
		            	$.post("${pageContext.request.contextPath}/page.offlineorderdel.ashx?oOId="+row.oOId,function(data){
			    			
			    			if(data.status == "1"){
			    				// 成功
			    				$.messager.alert('消息','操作成功 ','info');
			    				reloadgrid();
			    				
			    			
			    			}else{
			    					$.messager.alert('消息','<br>操作失败','info');
			    				
			    			}
			    				
			    		}); 
		            }  
		            else {  
		                 
		            }  
		        }); 


		}
	}
	function doProcess(index){
	    $('#grid').datagrid('selectRow',index);// 关键在这里  
	    var row = $('#grid').datagrid('getSelected'); 
	    if(row){ 
/* 	    	alert(row.oOId)
 */			location.href="${pageContext.request.contextPath}/page.offlineorderdetails.ashx?oId="+row.oOId;

		}
	}
	
	function formatterDate(value,row,index){
		if(value==''||null==value){
			return '';
		}
		var now = new Date(parseInt(value) * 1000);  
		var year=now.getYear()+1900; 
		var month=now.getMonth()+1; 
		var date=now.getDate(); 
		var hour=now.getHours(); 
		var minute=now.getMinutes();
		minute= (minute > 9) ? minute=minute : minute='0'+minute;
		//var second=now.getSeconds(); 
		return year+"-"+month+"-"+date+" "+hour+":"+minute; 
	}
	
	 function formateroOIsDone(value,row,index){
		if(value==false){
			return "否";
		}else{
			return "是";
		}
	} 
	


	


	$(function(){
		$('#add').click(doAdd);
/* 		$('#edit').click(doEdit);
 */		$('#search').click(doSearch);
 		//$('#sessionstaff').append(sessionstaff.sName);
 			

	});
	$(function(){
		// 先将body隐藏，再显示，不会出现页面刷新效果
		$("body").css({visibility:"visible"});
		
		$("#select_use").change(
			function() {
				var selected = $("#select_use").val();
				if(selected==1){
					$("#customer_tr").show();
				}else{
					$("#customer_tr").hide();
				}
			}		
		)
		// 收派标准数据表格
		$('#grid').datagrid( {
			iconCls : 'icon-forward',
			fit : true,
			border : false,
			rownumbers : true,
			singleSelect:true,
			striped : true,
			toolbar : '#tb',
			url : "${pageContext.request.contextPath}/page.OfflineOrderList.ashx",
			idField : 'oOId',
			columns : columns,
			pageList: [1,10,20,30],
			pagination:true,
			view:myview,
			emptyMsg:"<font size=5>暂无记录</font>"
			
		});
		
		//初始化员工窗口
		$('#outputWindow').window({
			
            title: '线下出库',
            width: 400,
            modal: true,
            shadow: true,
            closed: true,
            height: 400,
            resizable:true,
            closable:true
            
        });
		
		
		
		//提交表单
		$('#save').click(function(){
			
			var select = $('#oOReceiver').combobox('panel').find('.combobox-item-selected') 
			if(select.length==0){
				$.messager.alert('消息','请在下拉框中选取项目！','info');
				$('#oOReceiver').combobox('clear');
				return;
			}
			
				var oOPNumber = $("#oOPNumber").val();
				var selected = $("#select_use").val();
				var oOBuyer = $("#oOBuyer").combobox('getValue');

				
				if(oOPNumber>80){
					$.messager.alert('消息','单次操作不能超过80个 ','info');return;
				}
				
				if(!selected){
						$.messager.alert('消息','请选择出库类型 ','info');return;
				}
				
				if(selected==1){
					var select = $('#oOBuyer').combobox('panel').find('.combobox-item-selected') 
					if(select.length==0){
						$.messager.alert('消息','请在下拉框中选取项目！','info');
						$('#oOBuyer').combobox('clear');
						return;
					}
					if(oOBuyer){
						
					}else{
						$.messager.alert('消息','请选择客户 ','info');return;
					}
				}
				// 校验
				if($('#sform').form('validate')){
					// 1 、获得form 数据
					var params = $('#sform').serialize();
 					// 2、将数据发送给服务器 
					$.post("${pageContext.request.contextPath}/page.saveOOrder.ashx", params , function(data){
						if(data == "success"){
							// 成功
							$.messager.alert('消息','操作成功 ','info');
							// 关闭窗口
							$('#outputWindow').window('close');
							// 清除表单数据
							$('#outputWindow').form('clear');
							$('#outputWindow *').trigger('blur');// 离焦
							
							// 刷新表格数据 
							$('#grid').datagrid('reload');
						}else{
							$.messager.alert('消息','操作失败 ','info');
						}
							
					});
				}
				
			});	
		
		
	});
	
	
	
	var myview = $.extend({},$.fn.datagrid.defaults.view,{
		onAfterRender:function(target){
			$.fn.datagrid.defaults.view.onAfterRender.call(this,target);
			var opts = $(target).datagrid('options');
			var vc = $(target).datagrid('getPanel').children('div.datagrid-view');
			vc.children('div.datagrid-empty').remove();
			if (!$(target).datagrid('getRows').length){
				var d = $('<div class="datagrid-empty"></div>').html(opts.emptyMsg || 'no records').appendTo(vc);
				d.css({
					position:'absolute',
					left:0,
					top:50,
					width:'100%',
					textAlign:'center'
				});
			}
		}
	});

/* 	function generateBuyer(value){
		alert(111)
		if(value==1){
			$("#customer_tr").show();
		
		}else{
			$("#customer_tr").hide();
		} 
	} */
	
	
	$(function () {
        $('#oOReceiver').combobox({
        	url:'${pageContext.request.contextPath}/page.kuguanListAll.ashx?refreshcache=new Date().getTime()',
            valueField: 'sId',
            textField: 'sName',
            panelHeight:'300',
            onSelect: function (record) {
            	oOBuyer.combobox({
                    url:'${pageContext.request.contextPath}/page.buyerListAll.ashx?creatorId='+record.sId,
                    valueField: 'bId',
                    textField: 'bName',
                    panelHeight:'200'
                }).combobox('clear');
            },
            filter: function(q, row){  
    	        var opts = $(this).combobox('options');  
    	        return row[opts.textField].indexOf(q) >= 0;//这里改成>=即可在任意地方匹配  
    	    }
        });
        var oOBuyer = $('#oOBuyer').combobox({
            valueField: 'bId',
            textField: 'bName',
            filter: function(q, row){  
    	        var opts = $(this).combobox('options');  
    	        return row[opts.textField].indexOf(q) >= 0;//这里改成>=即可在任意地方匹配  
    	    }
        });
    });		
</script>	
</head>
<body class="easyui-layout" >
	<div region="center">
	
		<table id="grid">
		
		</table>
	</div>
	
	<div class="easyui-window"  id="outputWindow" collapsible="false" minimizable="false" maximizable="false" style="top:50px;left:200px">
		<div region="north" style="height:31px;overflow:hidden;" split="false" border="false" >
			<div class="datagrid-toolbar">
				<a id="save" icon="icon-save" href="#" class="easyui-linkbutton" plain="true" >保存</a>
			</div>
		</div>
		<div region="center" style="overflow:auto;padding:5px;" border="false">
			<form id="sform">
				<table class="table-edit" width="80%" align="center">
					<tr class="title">
						<td colspan="2">线下出库</td>
					</tr>
					<tr>
						<td>经手人</td>
						  <td id="sessionstaff">  
 						 <input class="easyui-combobox"    
									required="true"
									name="oOKuGuanId"
									id="kuguan"
									data-options="
											url:'${pageContext.request.contextPath}/page.kuguanListAll.ashx?refreshcache=new Date().getTime()',
											method:'get',
											valueField:'sId',
											textField:'sName',
											panelHeight:'300'
						"> </td>
					</tr>
					
					<tr>
						<td>接收人</td>
					<td>
						<input class="easyui-combobox"   
									required="true"
									name="oOReceiverId"
									id="oOReceiver"
									data-options="
											method:'get',
											valueField:'sId',
											textField:'sName',
											panelHeight:'300'
						"></td>
					</tr>
				
					
					
					
					<tr>
						<td>出库数量</td>
						<td><input id="oOPNumber" name="oOPNumber" type="text" class="easyui-validatebox"  required="true"  /></td>
					</tr>
					
					<tr>
						<td>产品用途</td>
						<td>
							<select name="oOType" id="select_use"  >
								   <option value ="0">员工自用</option>
								  <option value="1">销售提货</option>
							</select> 
							
							
						</td>
					</tr>
					
					
					 <tr id="customer_tr" hidden="hidden"> 
					
						<td>客户名称</td>
					<td>
						<input class="easyui-combobox"    
									name="oOBuyerId"
									id="oOBuyer"
									data-options="
											method:'get',
											valueField:'bId',
											textField:'bName',
											panelHeight:'200'
						"></td>
					</tr>
				
					<tr>
						<td>备注</td>
						<td><input name="oOReMark" type="text"   /></td>
					</tr>
					
				
					</table>
			</form>
		</div>
	</div>
	

	
		<div id="tb" style="padding:5px;height:auto">
		<div style="margin-bottom:5px">
			<a href="#" id="add" class="easyui-linkbutton" iconCls="icon-add" plain="true" >增加</a>
 
			接收者:<input id="searchcontent" class="easyui-textbox" style="width:80px"/>
			<a href="#"  id="search" class="easyui-linkbutton" iconCls="icon-search">搜索</a>
		
		</div>
	</div>

	
</body>
</html>